layui.use(['layer', 'form', 'admin', 'laydate', 'ax', 'formSelects','upload'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var upload = layui.upload;
    var laydate = layui.laydate;
    
    laydate.render({
        elem: '#test2'
        ,type: 'datetime'
     });
    
    laydate.render({
        elem: '#test3'
        ,type: 'datetime'
      });
    
    $(document).on('click', '#btnSelect', function(data) {
    	var userName = $("#userName").val();
    	if(userName == ''){
    		return;
    	}else{
        	// 异步请求
        	$.ajax({
        		url: Feng.ctxPath + "/modular/user/userinfo?userName="+userName,
        		type: "post",
        		async: false,//关闭异步请求
        		dataType: "json",
        		success: function (data) {
        			if(data.content.userinfo == ""){
        				Feng.error("没有该艺人信息！")
        			}else{
        				layer.open({
        		    		id:'addCont',
        		    		title:'选择艺人',
        		    		type:2,
        		    		area:['80%','80%'],
        		    		offset: 't',
        		    		content:Feng.ctxPath + "/modular/user/information?userName="+userName
        		    	});
        			}
        		}
        	,
        	error: function (data) {
        		Feng.error("添加失败！" + data.message)
        	}
        	
        	});
    	}
    });
    $(document).on('click', '#imgck', function(data) {
    	$("div#"+$(this).attr("name")).remove();
    });
    
    //普通图片上传
    var uploadInst = upload.render({
    	elem: '#test1'
            ,url: 'https://www.idjglobalapp.com/app-servce/v1/api/upload' //改成您自己的上传接口
            ,method: 'POST'
            ,done: function(res){
                //上传完毕
                if(res.code == 1){
                	layer.msg('上传成功') 
                	$('#demo1').attr('src', res.data);
                }
             }
    });
    
    // 表单提交事件
    form.on('submit(btnSubmit)', function (data) {
    	var comdata = [];
    	$('input[id="userNames"]').each(function (){
    		comdata.push($(this).attr("name"));
    	});
    	var pic =$("#demo1").attr("src");
    	var listsName = $("#listsName").val();
    	var content = $("#contes").val();
    	var startTime = $("#test2").val();
    	var endTime = $("#test3").val();
    	var data = {
    			user: JSON.stringify(comdata),
    			content: content,
    			pic: pic,
    			listsName: listsName,
    			startTime: startTime,
    			endTime: endTime,
    			listStatus:1
    	}
    	// 异步请求
    	$.ajax({
    		url: Feng.ctxPath + "/modular/lists/insert",
    		data: JSON.stringify(data),
    		type: "post",
    		contentType: "application/json;charset=UTF-8",
    		async: false,//关闭异步请求
    		dataType: "json",
    		success: function (data) {
    			if (data.code == 200) {
    				Feng.success("操作成功！");
    				//传给上个页面，刷新table用
    				// window.parent.location.reload();
    				admin.putTempData('formOk', true);
    				//关掉对话框
    				admin.closeThisDialog();
    			} else {
    				Feng.success("操作失败！" + data.message);
    			}
    		}
    	,
    	error: function (data) {
    		Feng.error("添加失败！" + data.message)
    	}
    	});
    });
    
});
